

let multer = require('koa-multer');
let path = require("path");

let storage = multer.diskStorage({
    destination: path.resolve('public/images'),
    filename: (ctx, file, cb) => {
        console.log('file :>> ', file);
        let num = file.originalname.lastIndexOf('.');
        let suffix = file.originalname.substring(num);
        cb(null, Date.now() + suffix);//写入自己的服务器文件夹
    }
});
let fileFilter = (ctx, file, cb) => {
    //过滤上传的后缀为txt的文件
    let num = file.originalname.lastIndexOf('.');
    let suffix = file.originalname.substring(num);
    if (!suffix in ['.jpg', '.png', '.jpeg', '.gif']) {
        cb(null, false);
    } else {
        cb(null, true);
    }
}
const upLoad = multer({ storage: storage, fileFilter: fileFilter });

module.exports = {
    upLoad
}

